<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="需求类别" prop="needCategory">
        <el-select v-model="queryParams.needCategory"
                   placeholder="请选择需求类别">
          <el-option
            v-for="dict in dict.type.need_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="经济带" prop="economic">
        <el-select v-model="queryParams.economic" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.economic_belt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="处理状态" prop="state2">
        <el-select v-model="queryParams.state2" placeholder="请选择状态">
          <el-option
            v-for="dict in status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择">
          <el-option
            v-for="dict in dict.type.audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-select v-model="queryParams.sort" clearable placeholder="请选择">
          <el-option
            v-for="dict in sortType"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分配用户" prop="userId2">
        <el-select v-model="queryParams.userId2" placeholder="请选择">
          <el-option
            v-for="dict in fenpeiList"
            :key="dict.userId"
            :label="dict.nickName"
            :value="parseInt(dict.userId)"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['information:need:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['information:need:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="needList" @selection-change="handleSelectionChange" height="600" fixed>
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center" prop="user.phonenumber"/>
      <el-table-column label="位置" align="center" prop="user.province"/>
      <el-table-column label="需求分类" align="center" prop="needCategory"/>
      <el-table-column label="需求标题" align="center" prop="title"/>
      <el-table-column label="摘要" align="center" prop="note"/>
      <el-table-column label="需求状态" align="center" prop="state2">
        <template slot-scope="scope">
          <div v-for="item in status" v-if="item.value === scope.row.state2">
            <span>{{ item.label }}</span>
            <span style="display: inline-block" v-if="scope.row.state2===0">🚩</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.audit_status" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="updateBy"/>
      <el-table-column label="上传时间" align="center" prop="createTime"/>
      <el-table-column width="220" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleLook(scope.row.id)"
          >预览
          </el-button>
          <el-button
            style=""
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['information:need:edit']"
          >
            <span v-if="$store.getters.userId===scope.row.userId">编辑</span>
            <span v-if="$store.getters.userId!==scope.row.userId">查看</span>
          </el-button>
          <el-button
            v-if="scope.row.state==0"
            style=""
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['information:need:edit']"
          >审核
          </el-button>
          <el-button
            v-if="scope.row.state==3"
            style=""
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['information:need:edit']"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleSolve(scope.row)"
            v-hasPermi="['information:need:edit']"
            v-if="scope.row.solve===1"
          >未解决
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleSolve(scope.row)"
            v-hasPermi="['information:need:edit']"
            v-if="scope.row.solve===0"
          >已解决
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTop(scope.row)"
            v-hasPermi="['information:need:edit']"
            v-if="scope.row.top===0"
          >置顶
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTop(scope.row)"
            v-hasPermi="['information:need:edit']"
            v-if="scope.row.top===1"
          >取消置顶
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业需求对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="759px" :close-on-click-modal='false' append-to-body center>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="需求封面" prop="cover">
          <image-upload :limit="1" v-model="form.cover"/>
        </el-form-item>
        <el-form-item label="需求标题" prop="title">
          <el-input class="kind" v-model="form.title" placeholder="请输入标题"/>
        </el-form-item>
        <el-form-item label="需求类别" prop="needCategory">
          <el-select class="kind" v-model="form.needCategory"
                     placeholder="请选择需求类别">
            <el-option
              v-for="dict in dict.type.need_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.needCategory=='招商落地需求'" label="落地区域" prop="address">
          <el-input class="kind" v-model="form.address" placeholder="请输入"/>
        </el-form-item>
        <el-form-item v-if="form.needCategory=='招商落地需求'" label="落地所需政策" prop="policy">
          <el-input class="kind" v-model="form.policy" placeholder="请输入"/>
        </el-form-item>
        <el-form-item v-if="form.needCategory=='招商落地需求'" label="就业岗位数量" prop="postNum">
          <el-input class="kind" v-model="form.postNum" placeholder="请输入"/>
        </el-form-item>
        <el-form-item v-if="form.needCategory=='招商落地需求'" label="税收" prop="taxation">
          <el-input class="kind" v-model="form.taxation" placeholder="请输入"/>
        </el-form-item>
        <el-form-item v-if="form.needCategory=='招商落地需求'" label="物理空间需求" prop="demand">
          <el-input class="kind" v-model="form.demand" placeholder="详细需求"/>
        </el-form-item>
        <el-form-item  :label="form.needCategory==='招商落地需求'?'落地项目介绍':'需求内容' ">
          <MyEditor class="kind" v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="需求状态" prop="state2">
          <el-select class="kind" v-model="form.state2" placeholder="请选择状态">
            <el-option
              v-for="dict in status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="需求摘要" prop="note">
          <el-input class="kind" type="textarea" v-model="form.note" placeholder="请输入摘要"/>
        </el-form-item>
        <el-form-item label="联盟会员" prop="isVip">
          <el-select class="kind" v-model="form.isVip" placeholder="请选择状态">
            <el-option
              v-for="dict in status2"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分配用户" prop="userId2">
          <el-select class="kind" v-model="form.userId2" placeholder="请选择分配用户">
            <el-option
              v-for="dict in fenpeiList"
              :key="dict.userId"
              :label="dict.nickName"
              :value="dict.userId+''"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="需求置顶" prop="top">-->
        <!--          <el-select class="kind" v-model="form.top" placeholder="请选择">-->
        <!--            <el-option-->
        <!--              v-for="dict in status4"-->
        <!--              :key="dict.value"-->
        <!--              :label="dict.label"-->
        <!--              :value="parseInt(dict.value)"-->
        <!--            ></el-option>-->
        <!--          </el-select>-->
        <!--        </el-form-item>-->
        <el-form-item label="修改人" prop="updateBy">
          <el-input class="kind" disabled v-model="form.updateBy" placeholder="请输入修改人"/>
        </el-form-item>
        <el-form-item label="上传时间" prop="createTime">
          <template slot-scope="scope">
            <span>{{ form.createTime }}</span>
          </template>
        </el-form-item>
        <el-form-item v-if="form.state!=0" label="审核备注" prop="detail">
          <el-input class="kind" disabled type="textarea" v-model="form.detail" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="$store.getters.userId===form.userId" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="open2" close-on-click-modal center width="750px">
      <el-form :model="form" label-width="130px">
        <el-form-item label="审核备注" prop="detail">
          <el-input class="kind" type="textarea" v-model="form.detail"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="success" v-if="form.state===0" @click="submitCheck(3)">审核通过</el-button>
        <el-button type="success" v-if="form.state===3" @click="submitCheck(1)">审核通过</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="danger" v-if="form.state===0" @click="submitCheck(2)">审核驳回</el-button>
        <el-button type="danger" v-if="form.state===3" @click="submitCheck(4)">审核驳回</el-button>
        <span style="width: 5%;display: inline-block"></span>
        <el-button type="info" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addNeed, delNeed, getNeed, listNeed, updateNeed} from "@/api/information/need";
import {listCenter} from "@/api/information/center";
import MyEditor from "@/components/wangEdiot/index.vue";
import {listUser} from "@/api/system/user";

export default {
  name: "Need",
  components: {MyEditor},
  dicts: ['link_type', 'audit_status', 'need_type', 'economic_belt'],
  data() {
    return {
      fenpeiList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业需求表格数据
      needList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      status: [
        {
          value: 0,
          label: "未读"
        },
        {
          value: 1,
          label: "已读未处理"
        }, {
          value: 2,
          label: "已读已处理"
        },
      ],
      status3: [
        {
          value: 0,
          label: "初审"
        },
        {
          value: 1,
          label: "审核通过"
        }, {
          value: 2,
          label: "审核失败"
        }, {
          value: 3,
          label: "复审"
        },
      ],
      sortType: [
        {
          value: 0,
          label: "按时间倒序"
        },
        {
          value: 1,
          label: "按时间正序"
        },
      ],
      status2: [
        {
          value: 0,
          label: "是"
        },
        {
          value: 1,
          label: "否"
        },
      ],
      status4: [
        {
          value: 1,
          label: "是"
        },
        {
          value: 0,
          label: "否"
        },
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        needCategory: null,
        title: null,
        content: null,
        note: null,
        sort: null,
        state: null,
        state2: null,
        dr: null,
        cover: null,
        num: null,
        recom: null,
        top: null,
        economic: null,
        userId2: null
      },
      // 表单参数
      form: {},
      centerList: [],
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户ID不能为空", trigger: "blur"}
        ],
        needCategory: [
          {required: true, message: "需求类别不能为空", trigger: "change"}
        ],
        title: [
          {required: true, message: "标题不能为空", trigger: "blur"},
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        content: [
          {required: true, message: "内容不能为空", trigger: "blur"}
        ],
        sort: [
          {required: true, message: "排序不能为空", trigger: "blur"}
        ],
        state: [
          {required: true, message: "状态不能为空", trigger: "blur"}
        ],
        dr: [
          {required: true, message: "是否删除不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "修改时间不能为空", trigger: "blur"}
        ],
        cover: [
          {required: true, message: "封面不能为空", trigger: "blur"}
        ],
        detail: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
        recom: [
          {required: true, message: "推荐不能为空", trigger: "blur"}
        ],
        note: [
          {max: 100, message: '长度必须小于 100', trigger: 'blur'}
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getCenter();
    listUser(this.addDateRange({
      pageNum: 1,
      pageSize: 9999999
    }, [])).then(response => {
        this.fenpeiList = response.rows;
        this.fenpeiList = this.fenpeiList.filter(t => t.userId != 1)
      }
    )
  },
  methods: {
    /** 已解决按钮操作 */
    handleSolve(row) {
      const id = row.id
      if (row.solve === 0) {
        this.$modal.confirm('是否已解决编号为"' + id + '"的数据项？').then(function () {
          row.solve = 1
          return updateNeed(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否未解决编号为"' + id + '"的数据项？').then(function () {
          row.solve = 0
          return updateNeed(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {
        });
      }

    },
    /** 已解决按钮操作 */
    handleTop(row) {
      const id = row.id
      if (row.top === 0) {
        this.$modal.confirm('是否置顶编号为"' + id + '"的数据项？').then(function () {
          row.top = 1
          return updateNeed(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {
        });
      } else {
        this.$modal.confirm('是否取消置顶编号为"' + id + '"的数据项？').then(function () {
          row.top = 0
          return updateNeed(row)
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("操作成功");
        }).catch(() => {
        });
      }

    },
    handleLook(id) {
      // window.location.href=`http://120.46.220.152/#/pages/news/details?id=`+id
      open(`http://120.46.220.152/#/pages/serviceHall/needsListDetails?id=` + id)
    },
    /** 提交审核 */
    submitCheck(state) {
      updateNeed({
        id: this.form.id,
        state: state,
        detail: this.form.detail,
        state2: 2
      }).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.open2 = false;
        this.getList();
      });
    },
    getCenter() {
      this.loading = true;
      listCenter().then(response => {
        this.centerList = response.data;
        this.loading = false;
      })
    },
    /** 查询企业需求列表 */
    getList() {
      this.loading = true;
      listNeed(this.queryParams).then(response => {
        this.needList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.open2 = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        needCategory: null,
        title: null,
        content: null,
        note: null,
        sort: null,
        state: null,
        dr: null,
        createTime: null,
        updateTime: null,
        cover: null,
        detail: null,
        num: null,
        recom: null,
        top: null,
        isVip: null,
        address:null,
        policy:null,
        demand:null,
        postNum:null,
        taxation:null,
        userId2:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //获取焦点事件
    focus(event) {
      event.enable(false);  //设置富文本编辑器不可编辑
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加企业需求";
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.reset();
      const id = row.id
      getNeed(id).then(response => {
        this.form = response.data;
        this.open2 = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      row.state2 = 1
      const id = row.id || this.ids
      getNeed(id).then(response => {
        this.form = response.data;
        this.open = true;
        if (this.$store.getters.userId !== this.form.userId) {
          this.title = "企业需求详情";
        } else {
          this.title = "修改企业需求";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if (this.form.state != 0) {
              this.form.state2 = 2
            }
            updateNeed(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.open2 = false;
              this.getList();
            });
          } else {
            this.form.state = 3
            addNeed(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除企业需求编号为"' + ids + '"的数据项？').then(function () {
        return delNeed(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('informfrontend/needBack/export', {
        ...this.queryParams
      }, `need_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>

.kind {
  width: 90%;
}
</style>
